<template>
    <div style="background-color:#f1f1f1">
        <mt-header title="红包">
            <router-link to="/myself" slot="left">
                <mt-button icon="back">返回</mt-button>
            </router-link>
        </mt-header>
        <el-tabs v-model="activeName" @tab-click="handleClick"  style="background-color:#fff">
            <el-tab-pane label="全部红包" name="first"></el-tab-pane>
            <el-tab-pane label="最近获取" name="second"></el-tab-pane>
            <el-tab-pane label="即将过期" name="third"></el-tab-pane>
        </el-tabs>
        <ul class="infinite-list redBagContainer" v-infinite-scroll="load" style="overflow:auto" infinite-scroll-disabled="close" :key="activeName">
            <div class="redBox infinite-list-item" v-for="(item,index) in redBag" :key="index">
                <div class="imgBox"><img :src="item.shopUrl"></div>
                <div class="messageBox">
                    <p class="title" v-if="item.shopId!=0&&item.type!='activity'">{{item.shopName}}专享红包</p>
                    <p class="title" v-if="item.shopId!=0&&item.type=='activity'">{{item.shopName}}盲盒</p>
                    <p class="title" v-if="item.shopId==0">{{item.shopName}}</p>
                    <p>{{item.startTime}}到{{item.endTime}}</p></div>
                <div class="reductionBox">
                    <p class="tip" v-if="item.tag==2">将到期</p><p class="tip" v-if="item.tag==1">新获取</p>
                    <p class="use">￥{{item.reduction/100}}</p><p>满{{item.fullReduction/100}}可用</p>
                </div>
                <div></div>
                <div>
                    <p v-if="item.shopId!=0">仅用于指定商家</p>
                    <p v-if="item.shopId==0">可用于任何商家</p>
                </div>
                <div class="buttonBox"><button>去使用</button></div>
            </div>
        </ul>
        <router-link to='historyRedBag'>
            <button class="historyRedBagClass">历史红包</button>
        </router-link>
    </div>
</template>
<script>
    export default{
        data(){
            return{
                activeName:"first",
                redBag:[],
                currentPage:0,
                size:5,
                close:false,
            }
        },
        mounted(){
            this.initMessage();
            this.getAll();
        },
        methods:{
            handleClick(tab,event){
                if(tab.name=="first"){
                    console.log('这是全部红包区');
                    this.initMessage();
                    this.getAll();
                }else if(tab.name=="second"){
                    console.log('这是最近获取区');
                    this.initMessage();
                    this.getRecent();
                }else if(tab.name=="third"){
                    console.log('这是即将过期区');
                    this.initMessage();
                    this.getDeadline();
                }
            },
            initMessage(){
                this.redBag=[];
                this.close=false;
                this.currentPage=0;
            },
            getAll(){
                let tokenStr=window.sessionStorage.getItem("tokenStr");
                this.currentPage=this.currentPage+1;
                let postData={
                    currentPage:this.currentPage,
                    size:this.size,
                };
                this.getRequest('/redPaper/getAllRedParper',postData,tokenStr).then(response=>{
                    if(response.code==200){
                        let list=response.obj.data;
                        for(let i=0;i<list.length;i++){
                            list[i].startTime=list[i].startTime.replace(/T/," ");
                            list[i].endTime=list[i].endTime.replace(/T/," ");
                            list[i].startTime=list[i].startTime.slice(0,16);
                            list[i].endTime=list[i].endTime.slice(0,16);
                            list[i].shopUrl="/file"+list[i].shopUrl;
                            let index=list[i].redParperId.indexOf("_");
                            list[i].type=list[i].redParperId.slice(0,index);
                            this.redBag.push(list[i]);
                        }
                        if(this.redBag.length>=response.obj.all)
                            this.close=true;
                    }
                })
            },
            getRecent(){
                let tokenStr=window.sessionStorage.getItem("tokenStr");
                this.currentPage=this.currentPage+1;
                let postData={
                    currentPage:this.currentPage,
                    size:this.size,
                };
                this.getRequest('/redPaper/getRecentRedParper',postData,tokenStr).then(response=>{
                    if(response.code==200){
                        let list=response.obj.data;
                        for(let i=0;i<list.length;i++){
                            list[i].startTime=list[i].startTime.replace(/T/," ");
                            list[i].endTime=list[i].endTime.replace(/T/," ");
                            list[i].startTime=list[i].startTime.slice(0,16);
                            list[i].endTime=list[i].endTime.slice(0,16);
                            list[i].shopUrl="/file"+list[i].shopUrl;
                            let index=list[i].redParperId.indexOf("_");
                            list[i].type=list[i].redParperId.slice(0,index);
                            this.redBag.push(list[i]);
                        }
                        if(this.redBag.length>=response.obj.all)
                            this.close=true;
                    }
                })
            },
            getDeadline(){
                let tokenStr=window.sessionStorage.getItem("tokenStr");
                this.currentPage=this.currentPage+1;
                let postData={
                    currentPage:this.currentPage,
                    size:this.size,
                };
                this.getRequest('/redPaper/getWillExpireRedParper',postData,tokenStr).then(response=>{
                    if(response.code==200){
                        let list=response.obj.data;
                        for(let i=0;i<list.length;i++){
                            list[i].startTime=list[i].startTime.replace(/T/," ");
                            list[i].endTime=list[i].endTime.replace(/T/," ");
                            list[i].startTime=list[i].startTime.slice(0,16);
                            list[i].endTime=list[i].endTime.slice(0,16);
                            list[i].shopUrl="/file"+list[i].shopUrl;
                            let index=list[i].redParperId.indexOf("_");
                            list[i].type=list[i].redParperId.slice(0,index);
                            this.redBag.push(list[i]);
                        }
                        if(this.redBag.length>=response.obj.all)
                            this.close=true;
                    }
                })
            },
            load(){
                //发送请求给后端，往redBag里添加新东西
                if(this.activeName=="first")
                    this.getAll();
                else if(this.activeName=="second")
                    this.getRecent();
                else if(this.activeName=="third")
                    this.getDeadline();
            },
        }
    }
</script>
<style lang="scss" scoped>
    @import 'mint-ui/lib/style.css';
    @import '../style/mixin';
    *{
        margin: 0;
        padding: 0;
    }
    /deep/ .el-tabs__item{
        font-size:18px;
    }
    /deep/ .el-tabs__header{
        margin:0;
    }
    /deep/ .el-tabs__nav{
        padding:0.2rem;
    }
    .redBagContainer{
        @include width-height(100%,83vh);
    }
    .redBox{
        @include width-height(100%,7rem);
        display: grid;
        grid-template-rows: 66.66% 33.33%;
        grid-template-columns: 15% 62% 23%;
        padding-top: 0.5rem;
        border-radius: 1.2rem;
        background-color: #fff;
        border-bottom: 2px solid #f1f1f1;
        padding-bottom: 0.2rem;
        margin-top: 0.2rem;
        .imgBox{
            text-align: center;
            img{
                @include width-height(50px,50px);
                margin-top: 0.5rem;
                border-radius: 0.3rem;
            }
        }
        .messageBox{
            .title{
                padding-top: 0.5rem;
                @include font-size-color(1.2rem,#000);
                font-weight: bold;
            }
            p{
                padding-top: 0.3rem;
            }
        }
        .reductionBox{
            text-align: center;
            position: relative;
            .use{
                padding-top: 0.7rem;
                @include font-size-color(1.8rem,rgba(231, 73, 25, 0.87));
                font-weight: bold;
            }
            .tip{
                display: inline;
                padding: 0.1rem 0.2rem 0.1rem 0.2rem;
                border-radius: 0.4rem;
                position: absolute;
                right: 0;
                top:-0.5rem;
                background-color: rgba(255, 219, 209, 0.87);
                @include font-size-color(0.8rem,rgba(231, 73, 25, 0.87));
            }
        }
        p{
            @include font-size-color(1rem,#666);
        }
        .buttonBox{
            text-align: center;
            button{
                background-color: rgba(231, 73, 25, 0.87);
                @include width-height(80%,2rem);
                border: none;
                border-radius: 1rem;
                @include font-size-color(0.9rem,#fff);
            }
        }
    }
    .historyRedBagClass{
        @include width-height(100%,2.5rem);
        border: none;
        @include font-size-color(1.2rem,#666);
    }
</style>
